package com.test.xml;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.util.List;

/**
 * @Description:
 * @Author: yangzhiwei
 * @Date: 2021/4/1 16:50
 */
public class XMLLearning {
    public static void main(String[] args) throws DocumentException {
        SAXReader reader = new SAXReader();
        File file = new File("C:\\Users\\fang.com\\IdeaProjects\\WorkingTest\\featureTest\\src\\main\\java\\com\\test\\xml\\myxml.xml");
        System.out.println(file.exists());
        //InputStream in = XMLLearning.class.getClassLoader().getResourceAsStream(file);
        Document doc = reader.read(file);
        Element root = doc.getRootElement();
        System.out.println(root);
        List<Element> childElements = root.elements();
        for (Element child : childElements) {
            List<Attribute> attributes = child.attributes();
            for (Attribute attribute : attributes) {
                System.out.println(attribute.getName() +  attribute.getValue());
            }
            //System.out.println(element.attributeValue("from"));
        }
       // for ()
     //   for ()

    }

    @SuppressWarnings("unchecked")
    public static void readNode(Element root, String prefix) {
        if (root == null) return;
        // 获取属性
        List<Attribute> attrs = root.attributes();
        if (attrs != null && attrs.size() > 0) {
            System.err.print(prefix);
            for (Attribute attr : attrs) {
                System.err.print(attr.getValue() + " ");
            }
            System.err.println();
        }
        // 获取他的子节点
        List<Element> childNodes = root.elements();
        prefix += "\t";
        for (Element e : childNodes) {
            readNode(e, prefix);
        }
    }
}
